/*
 * Author: 凌逆战 | Never
 * Data: 2023/9/10
 * Description: 读取16kHz音频数据，对16khz的音频数据进行高通滤波，然后保存
 */
#include <stdio.h>
#include "resampler.h"
#include "common.h"
#include "data_utils.h"

int main(int argc, char const* argv[]) {
    FILE* fp_16kHz = fopen("../file/16kHz_output.pcm", "rb");
    if (fp_16kHz == NULL) {
        printf("open file failed!\n");
        return -1;
    }
    FILE* fp_16kHz_highpass = fopen("../file/16kHz_highpass.pcm", "wb");
    if (fp_16kHz_highpass == NULL) {
        printf("open file failed!\n");
        fclose(fp_16kHz_highpass);
        return -1;
    }

    unsigned long read_samples;
    float highpass_mem[1 * PSAP_BIQUAD_ORDER] = {0.0f};
    short in_buffer[FRAME_LEN_16K] = {0};
    short out_buffer[FRAME_LEN_16K] = {0};
    float filter_buffer[FRAME_LEN_16K] = {0.0f};
    while ((read_samples = fread(in_buffer, sizeof(short), FRAME_LEN_16K, fp_16kHz)) != 0) {
        if (read_samples != FRAME_LEN_16K) {
            printf("read_samples: %ld\n", read_samples);
            break;
        }
        convert_i16_to_f32(in_buffer, FRAME_LEN_16K, filter_buffer);
        HighPass_fc70kHz(filter_buffer, FRAME_LEN_16K, highpass_mem);

        convert_f32_to_i16(filter_buffer, FRAME_LEN_16K, out_buffer);
        fwrite(&out_buffer, sizeof(short), FRAME_LEN_16K, fp_16kHz_highpass);
    }
    printf("done!\n");
    return 0;
}
